$(function() {
	tableInit();
	validationInit();
	suggestionInit();
});
function suggestionInit() {
	// fc suggestion
	var fcSuggestion = new Bloodhound({
		datumTokenizer : Bloodhound.tokenizers.obj.whitespace('value'),
		queryTokenizer : Bloodhound.tokenizers.whitespace,
		limit : 10,
		remote : {
			url : window.basePath + "padding/suggest/fc/%QUERY",
			filter : function(list) {
				return $.map(list, function(fc) {
					return {
						fcName : fc
					};
				});
			}
		}

	});
	fcSuggestion.initialize();
	$('.suggestfc').typeahead(null, {
		name : 'fc',
		displayKey : 'fcName',
		source : fcSuggestion.ttAdapter()
	});

	// ship method suggestion
	var smSuggestion = new Bloodhound({
		datumTokenizer : Bloodhound.tokenizers.obj.whitespace('value'),
		queryTokenizer : Bloodhound.tokenizers.whitespace,
		limit : 10,
		remote : {
			url : window.basePath + "padding/suggest/sm/%QUERY",
			filter : function(list) {
				return $.map(list, function(sm) {
					return {
						smName : sm
					};
				});
			}
		}

	});
	smSuggestion.initialize();
	$('.suggestSM').typeahead(null, {
		name : 'sm',
		displayKey : 'smName',
		source : smSuggestion.ttAdapter()
	});

	// padding suggestion
	var smSuggestion = new Bloodhound({
		datumTokenizer : Bloodhound.tokenizers.obj.whitespace('value'),
		queryTokenizer : Bloodhound.tokenizers.whitespace,
		limit : 10,
		remote : {
			url : window.basePath + "padding/suggest/padding/%QUERY",
			filter : function(list) {
				return $.map(list, function(padding) {
					return {
						paddingName : padding
					};
				});
			}
		}

	});
	smSuggestion.initialize();

	$('.suggestPadding').typeahead(null, {
		name : 'padding',
		displayKey : 'paddingName',
		source : smSuggestion.ttAdapter()
	});

	// province suggestion
	var provinceSuggestion = new Bloodhound({
		datumTokenizer : Bloodhound.tokenizers.obj.whitespace('value'),
		queryTokenizer : Bloodhound.tokenizers.whitespace,
		limit : 10,

		remote : {
			url : window.basePath + "padding/suggest/province/%QUERY",
			filter : function(list) {
				return $.map(list, function(province) {
					return {
						provinceName : province
					};
				});
			}
		}

	});
	provinceSuggestion.initialize();

	$('.suggestProvince').typeahead(null, {
		name : 'province',
		displayKey : 'provinceName',
		source : provinceSuggestion.ttAdapter()
	});

	// city suggestion
	var citySuggestion = new Bloodhound({
		datumTokenizer : Bloodhound.tokenizers.obj.whitespace('value'),
		queryTokenizer : Bloodhound.tokenizers.whitespace,
		limit : 10,
		remote : {
			url : window.basePath + "padding/suggest/city/%QUERY",
			filter : function(list) {
				return $.map(list, function(city) {
					return {
						cityName : city
					};
				});
			}
		}

	});
	citySuggestion.initialize();
	$('.suggestCity').typeahead(null, {
		name : 'city',
		displayKey : 'cityName',
		source : citySuggestion.ttAdapter()
	});
	// district suggestion
	var districtSuggestion = new Bloodhound({
		datumTokenizer : Bloodhound.tokenizers.obj.whitespace('value'),
		queryTokenizer : Bloodhound.tokenizers.whitespace,
		limit : 10,
		remote : {
			url : window.basePath + "padding/suggest/district/%QUERY",
			filter : function(list) {
				return $.map(list, function(district) {
					return {
						districtName : district
					};
				});
			}
		}

	});
	districtSuggestion.initialize();
	$('.suggestDistrict').typeahead(null, {
		name : 'district',
		displayKey : 'districtName',
		source : districtSuggestion.ttAdapter()
	});
}

function tableInit() {
	var mytable = $('#exsitPadding').DataTable({
		"processing" : true,
		"serverSide" : true,
		"deferRender" : true,
		"dom" : 'tr<"bottom"ip>',
		"order" : [ [ 0, "desc" ] ],
		"ajax" : {
			"url" : window.basePath + "exsit/list",
			"type" : "POST"
		},
		"language" : {
			"zeroRecords" : "empty list!",
			"infoEmpty" : "No records available",
			"infoFiltered" : "(filtered from _MAX_ total records)"
		},
		"columnDefs" : [ {
			"targets" : [ 0 ],
			"visible" : false,
		}, {
			"targets" : [ 1 ],
		}, {
			"targets" : [ 2 ],
		}, {
			"targets" : [ 3 ],
		}, {
			"targets" : [ 4 ],
		}, {
			"targets" : [ 5 ],
		}, {
			"targets" : [ 6 ],
		}, {
			"targets" : [ 7 ],
		}, {
			"targets" : [ 8 ],
		}, {
			"targets" : [ 9 ],
			"render" : function(data, type, row) {
				return '<a href="' + row[10] + '">' + data + '</a>';
			},
		}, {
			"targets" : [ 10 ],
			"render" : function(data, type, row) {
				return row[11];
			},
		} ],
	});

	// init filter
	$('input.column_filter').on('change keyup', function() {
		filterColumn($(this).attr('data-column'));
	});
	$(".column_filterD").on("dp.change", function(e) {
		filterColumn($(this).attr('data-column'));
	});
	function filterColumn(i) {
		mytable.column(i).search($('#col' + i + '_filter').val(), "", "")
				.draw();
	}

	onlyDateOption = {
		pickTime : false,
		defaultDate : "",
	};

	// init DatePicker
	function initDatePickerPair(start, end) {
		$(start).datetimepicker(onlyDateOption);
		$(end).datetimepicker(onlyDateOption);
		$(start).on("dp.change", function(e) {
			$(end).data("DateTimePicker").setMinDate(e.date);
			$(this).blur();
		});
		$(end).on("dp.change", function(e) {
			$(start).data("DateTimePicker").setMaxDate(e.date);
			$(this).blur();
		});
	}
	initDatePickerPair('#startDate', '#endDate');
	initDatePickerPair('#col7_filter', '#col8_filter');
	initDatePickerPair('#startDateE', '#endDateE');

	// buttion init
	$("#deployNewPadding").click(function() {
		$("#deployNewPaddingModel").modal('show');

	});

}

function validationInit() {
	validationOption = {
		rules : {
			fc : {
				minlength : 1,
				maxlength : 4,
				required : true
			},
			shipmethod : {
				minlength : 1,
				maxlength : 30,
				required : true
			},
			padding : {
				minlength : 1,
				maxlength : 3,
				required : true,
				digits : true
			},
			province : {
				minlength : 1,
				maxlength : 30,
				required : true
			},
			city : {
				minlength : 1,
				maxlength : 30,
				required : true
			},
			district : {
				minlength : 1,
				maxlength : 30,
				required : true
			},
		},
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		unhighlight : function(element) {
			$(element).closest('.form-group').removeClass('has-error');
		},
		errorElement : 'span',
		errorClass : 'help-block',
		errorPlacement : function(error, element) {
			if (element.parent('.input-group').length) {
				error.insertAfter(element.parent());
			} else {
				error.insertAfter(element);
			}
		}

	};
	// $('#addForm').validate(validationOption);
	// $('#editForm').validate(validationOption);
	// if (window.errorMsg) {
	// $("#alert-warning").html(window.errorMsg).removeClass("hidden").hide()
	// .fadeIn("slow");
	// $("#alert-warning").fadeOut(5000);
	// }
}
